﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class PipeTypeMagForm : System.Windows.Forms.Form
	{
		public PipeTypeMagForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.btnNew.Image = Resources.AddNew16;
			this.btnEdit.Image = Resources.modify16;
			this.btnDelete.Image = Resources.delete16;
		}

		public PipeTypeMagForm(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			this.initLstPipeType();
			base.Icon = Resource.YArchitechIcon;
			this.btnNew.Image = Resources.AddNew16;
			this.btnEdit.Image = Resources.modify16;
			this.btnDelete.Image = Resources.delete16;
		}

		public PipeTypeMagForm(Autodesk.Revit.DB.Document doc, string typeNmae)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			this.type = typeNmae;
			this.initLstPipeType(typeNmae);
			if (typeNmae == "风")
			{
				this.Text = "风管类型管理";
			}
			else
			{
				this.Text = "管道类型管理";
			}
			base.Icon = Resource.YArchitechIcon;
			this.btnNew.Image = Resources.AddNew16;
			this.btnEdit.Image = Resources.modify16;
			this.btnDelete.Image = Resources.delete16;
		}

		private void initLstPipeType(string typeNmae)
		{
			this.lsbPipeType.Items.Clear();
			List<Element> list = new List<Element>();
			if (typeNmae == "水")
			{
				list = YJKElementFilter.GetElementByClass(typeof(PipeType), this.doc);
			}
			else if (typeNmae == "风")
			{
				list = YJKElementFilter.GetElementByClass(typeof(DuctType), this.doc);
			}
			this.lsbPipeType.DisplayMember = "Name";
			this.lsbPipeType.ValueMember = "Value";
			foreach (Element element in list)
			{
				ListItem item = new ListItem(element.Name, element.Id.IntegerValue.ToString());
				this.lsbPipeType.Items.Add(item);
			}
			if (this.lsbPipeType.Items.Count > 0 && this.lsbPipeType.Items[0] != null)
			{
				this.lsbPipeType.SelectedItem = this.lsbPipeType.Items[0];
			}
		}

		private void initLstPipeType()
		{
			this.lsbPipeType.Items.Clear();
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipeType), this.doc);
			this.lsbPipeType.DisplayMember = "Name";
			this.lsbPipeType.ValueMember = "Value";
			foreach (Element element in elementByClass)
			{
				ListItem item = new ListItem(element.Name, element.Id.IntegerValue.ToString());
				this.lsbPipeType.Items.Add(item);
			}
			if (this.lsbPipeType.Items.Count > 0 && this.lsbPipeType.Items[0] != null)
			{
				this.lsbPipeType.SelectedItem = this.lsbPipeType.Items[0];
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("删除类型后，所有此类型的管道也将被删除，确定要删除吗？", "盈建科提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
			{
				return;
			}
			if (this.lsbPipeType.SelectedItem != null)
			{
				try
				{
					this.doc.Delete(new ElementId(int.Parse((this.lsbPipeType.SelectedItem as ListItem).Value)));
					this.doc.Regenerate();
					this.initLstPipeType(this.type);
				}
				catch (Exception)
				{
					YJKMessageBox.Information("删除失败，管道至少存在一种类型");
				}
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			if (this.lsbPipeType.SelectedItem != null)
			{
				ElementId elementId = new ElementId(int.Parse((this.lsbPipeType.SelectedItem as ListItem).Value));
				Element elementById = this.doc.GetElementById(elementId);
				EditPipeTypeForm editPipeTypeForm = new EditPipeTypeForm(this.doc, elementById);
				DialogResult dialogResult = editPipeTypeForm.ShowDialog();
				if (DialogResult.OK == dialogResult)
				{
					string pipeTypeName = editPipeTypeForm.pipeTypeName;
					elementById.Name = pipeTypeName;
					this.doc.Regenerate();
					this.initLstPipeType(this.type);
					this.SelectedItemByName(pipeTypeName);
				}
			}
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			if (this.lsbPipeType.SelectedItem != null)
			{
				ElementId elementId = new ElementId(int.Parse((this.lsbPipeType.SelectedItem as ListItem).Value));
				Element elementById = this.doc.GetElementById(elementId);
				NewPipeTypeForm newPipeTypeForm = new NewPipeTypeForm(this.doc, elementById);
				DialogResult dialogResult = newPipeTypeForm.ShowDialog();
				if (DialogResult.OK == dialogResult)
				{
					this.initLstPipeType(this.type);
					string pipeTypeName = newPipeTypeForm.pipeTypeName;
					this.SelectedItemByName(pipeTypeName);
				}
			}
		}

		private void SelectedItemByName(string name)
		{
			foreach (object obj in this.lsbPipeType.Items)
			{
				ListItem listItem = (ListItem)obj;
				if (name == listItem.Name)
				{
					this.lsbPipeType.SelectedItem = listItem;
					break;
				}
			}
		}

		private Document doc;

		private string type = "";
	}
}
